"""
  Created by Amor on 2018-08-13
"""
import django_filters
from django.db.models import Q

from .models import Goods

__author__ = '骆杨'


class GoodsFilter(django_filters.rest_framework.FilterSet):
    """
    商品的过滤器
    """
    pricemin = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte', help_text='最大价格')
    pricemax = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte', help_text='最小价格')
    top_category = django_filters.NumberFilter(method='top_category_filter', help_text='类别')

    def top_category_filter(self, queryset, name, value):
        queryset = queryset.filter(Q(category_id=value) |
                                   Q(category__parent_category_id=value) |
                                   Q(category__parent_category__parent_category_id=value))
        return queryset

    class Meta:
        model = Goods
        fields = ['pricemin', 'pricemax', 'top_category', 'is_hot', 'is_new']
